(function()
{
    function findElementByNameAttribute( tagName, parEl, name )
    {
        var taNodes = parEl.getElementsByTagName(tagName);
        for (var ii = 0; ii < taNodes.length; ii++)
        {
            var childEl = taNodes[ii];
            var msg = childEl.getAttribute("name");
            if (name == msg)
                return childEl;
        }
        return null;
    }

    var GmailOperator = lucidapi.CryptoOperator.extend(function(classref) { return {
        initialize: function(bodyTa)
        {
            classref.superClass.initialize.call(this,window);
            this.bodyTa = bodyTa;
        },
        getInvocationTarget: function()
        {
            return this.bodyTa.value;
        },
        handleInplaceResult: function( resultobj )
        {
            var stuff = resultobj.m_result;
            this.bodyTa.value = stuff;
            return true;
        }
    }});

    function handleGmailEncrypt()
    {
        var el = this;
        var fromstr = "";
        var bodyTa = findElementByNameAttribute("textarea", el.ownerDocument, "body");
        if (lucidapi.config.google_mail_autopop_sigkey)
        {
            var fromsel = findElementByNameAttribute("select", el.ownerDocument, "from");
            if (fromsel)
            {
                fromstr = fromsel.value;
            }
            else
            {
                fromsel = findElementByNameAttribute("input", el.ownerDocument, "from");
                if (fromsel)
                    fromstr = fromsel.value;
            }
        }
        var preselect = [];
        if (lucidapi.config.google_mail_autopop_enckey)
        {
            var tosrchstr = findElementByNameAttribute("textarea", el.ownerDocument, "to").value;
            tosrchstr += "," + findElementByNameAttribute("textarea", el.ownerDocument, "cc").value;
            tosrchstr += "," + findElementByNameAttribute("textarea", el.ownerDocument, "bcc").value;
            var emailRe = /\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.((com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)|\w{2})/
            var mymatch = emailRe.exec(tosrchstr);
            while (mymatch)
            {
                var lastmatch = mymatch[0];
                preselect.push(lastmatch);
                tosrchstr = tosrchstr.substr(mymatch.index + lastmatch.length);
                mymatch = emailRe.exec(tosrchstr);
            }
        }
        var cryptoCallback = new GmailOperator(bodyTa);

        cryptoCallback.invokeEncryption({preselect:preselect, presig:fromstr});
    }

    function addComposeButtons( parEl )
    {
        var retval = false;
        var taNodes = parEl.getElementsByTagName("button");
        for (var ii = 0; ii < taNodes.length; ii++)
        {
            var childEl = taNodes[ii];
            var msg = childEl.getAttribute("name");
            if (!msg)
            {
                msg = childEl.childNodes[0].nodeValue;
            }
            if ("Discard" == msg)
            {
                var parNode = childEl.parentNode;
                if (parNode.getAttribute("lucidwashere") != "true")
                {
                    parNode.setAttribute("lucidwashere", "true");
                    var encryptButton = parNode.ownerDocument.createElement("button");
                    encryptButton.textContent = "Encrypt";
                    encryptButton.addEventListener("click", handleGmailEncrypt, false);
                    parNode.appendChild(encryptButton);
                    retval = true;
                }
            }
        }
        return retval;
    }
    ;

    function handleBubblingLoad( event )
    {
        var elem = event.target;
        var name = elem.localName;
        if (name == "DIV")
        {
            addComposeButtons(elem.ownerDocument);
        }
    }

    function handleGmailInjection( event )
    {
        if (event.originalTarget instanceof HTMLDocument)
        {
            var doc = event.originalTarget;
            if (doc.domain == "mail.google.com")
                doc.addEventListener("DOMNodeInserted", handleBubblingLoad, false);
        }
    }

    if (lucidapi.config.google_mail_enabled && lucidapi.config.google_mail_encryptbutton)
    {
        window.addEventListener("load", function()
        {
            gBrowser.addEventListener("load", handleGmailInjection, true);
        }, false);
    }

})();

